package com.marvels.approve.common.service.hystrix;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.marvels.approve.common.service.ProductConsumerService;
import com.marvels.approve.loan.dto.McptConsultingFeeDTO;
import com.marvels.approve.loan.dto.McptProductTypeDto;
import com.marvels.approve.loan.dto.McptRateDTO;
import com.marvels.approve.loan.dto.McptRiskFeeDTO;
import com.marvels.approve.process.dto.McptManagementFeeDTO;
import com.marvels.approve.process.dto.McptProduct;
import com.marvels.approve.process.dto.McptProductFundRelationDto;
import com.marvels.approve.process.dto.McptProductInfoDTO;
import com.marvels.common.utils.MarvelsLogUtil;
import com.marvels.product.param.api.ProductConsultFeeApi;
import com.marvels.product.param.service.ProductItemService;

import cn.hutool.core.bean.BeanUtil;

@Service
public class ProductConsumerServiceHystrix implements ProductConsumerService {
	
	@Autowired
	private ProductConsultFeeApi productConsultFeeApi;
	
	@Autowired
	private ProductItemService productItemService;
	
	@Override
	public McptProduct getProduct(String ProductCode) {
		try {
			com.marvels.product.productmanage.dto.McptProductTypeDto list= productConsultFeeApi.getProductType(Long.getLong(ProductCode));
			McptProduct result = JSONObject.parseObject(JSONObject.toJSONString(list), McptProduct.class);
			MarvelsLogUtil.getInstance().info("ProductConsumerService.getProduct direct call success!");
			return result;
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("ProductConsumerService.getProduct error", e);
			return null;
		}
	}

	@Override
	public McptRiskFeeDTO getEntityByPrimaryKey(Long riskFeeId) {
		try {
			com.marvels.product.param.dto.McptRiskFeeDTO apiResult = productConsultFeeApi.getEntityByPrimaryKey(riskFeeId);
			McptRiskFeeDTO result = JSONObject.parseObject(JSONObject.toJSONString(apiResult), McptRiskFeeDTO.class);
			MarvelsLogUtil.getInstance().info("ProductConsumerService.getEntityByPrimaryKey direct call success!");
			return result;
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("ProductConsumerService.getEntityByPrimaryKey error", e);
			return null;
		}
	}

	@Override
	public McptManagementFeeDTO getMngfee(Long managementFeeId) {
		try {
			com.marvels.product.param.dto.McptManagementFeeDTO apiResult = productConsultFeeApi.getMngfee(managementFeeId);
			McptManagementFeeDTO result = JSONObject.parseObject(JSONObject.toJSONString(apiResult), McptManagementFeeDTO.class);
			MarvelsLogUtil.getInstance().info("ProductConsumerService.getMngfee direct call success!");
			return result;
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("ProductConsumerService.getMngfee error", e);
			return null;
		}
	}

	@Override
	public McptConsultingFeeDTO getConsultingFee( Long consultingFeeId)  {
		try {
			com.marvels.product.param.dto.McptConsultingFeeDTO apiResult = productConsultFeeApi.getConsultingFee(consultingFeeId);
			McptConsultingFeeDTO result = JSONObject.parseObject(JSONObject.toJSONString(apiResult), McptConsultingFeeDTO.class);
			MarvelsLogUtil.getInstance().info("ProductConsumerService.getConsultingFee direct call success!");
			return result;
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("ProductConsumerService.getConsultingFee error", e);
			return null;
		}
	}

	@Override
	public McptRateDTO getInterestRate(Long rateId)  {
		try {
			com.marvels.product.param.dto.McptRateDTO apiResult = productConsultFeeApi.getInterestRate(rateId);
			McptRateDTO result = JSONObject.parseObject(JSONObject.toJSONString(apiResult), McptRateDTO.class);
			MarvelsLogUtil.getInstance().info("ProductConsumerService.getInterestRate direct call success!");
			return result;
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("ProductConsumerService.getInterestRate error", e);
			return null;
		}
	}

	@Override
	public List<McptProductFundRelationDto> getProductFundingSource(McptProductFundRelationDto mcpdto) {
		try {
			com.marvels.product.productmanage.dto.McptProductFundRelationDto param = new com.marvels.product.productmanage.dto.McptProductFundRelationDto();
			BeanUtil.copyProperties(mcpdto, param);
			List<com.marvels.product.productmanage.dto.McptProductFundRelationDto> apiResult = productConsultFeeApi.getProductFundingSource(param);
			List<McptProductFundRelationDto> result = JSONObject.parseArray(JSONObject.toJSONString(apiResult), McptProductFundRelationDto.class);
			MarvelsLogUtil.getInstance().info("ApplyProvideConsumerApi.getProductFundingSource direct call success!");
			return result;
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("ProductConsumerService.getProductFundingSource error", e);
			return null;
		}
	}

	@Override
	public McptProductTypeDto getProductType(String ProductCode) {
		try {
			com.marvels.product.productmanage.dto.McptProductTypeDto list= productConsultFeeApi.getProductType(Long.getLong(ProductCode));
			McptProductTypeDto result = JSONObject.parseObject(JSONObject.toJSONString(list), McptProductTypeDto.class);
			MarvelsLogUtil.getInstance().info("ProductConsumerService.getProductType direct call success!");
			return result;
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("ProductConsumerService.getProductType error", e);
			return null;
		}
	}

	@Override
	public List<McptProductInfoDTO> selectProductInfo(McptProductInfoDTO mcpdto) {
		try {
			com.marvels.product.productmanage.dto.McptProductInfoDTO param = new com.marvels.product.productmanage.dto.McptProductInfoDTO();
			BeanUtil.copyProperties(mcpdto, param);
			List<com.marvels.product.productmanage.dto.McptProductInfoDTO> apiResult = productItemService.selectProductInfo(param);
			List<McptProductInfoDTO> result = JSONObject.parseArray(JSONObject.toJSONString(apiResult), McptProductInfoDTO.class);
			MarvelsLogUtil.getInstance().info("ApplyProvideConsumerApi.selectProductInfo direct call success!");
			return result;
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("ProductConsumerService.selectProductInfo error", e);
			return null;
		}
	}

}
